package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     *
     * 自动填充切入点
     *
     */

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void AutoFillPointcut() {}

    @Before("AutoFillPointcut()")
    public void before(JoinPoint joinPoint) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        log.info("AutoFillPointcut before ");

        HashMap<Object, Method[]> methodHashMap = new HashMap<>();
        Object[] args = joinPoint.getArgs();
        //存放所有方法和参数对象
        for (Object arg : args) {
            Class<?> argClass = arg.getClass();
            Method[] methods = argClass.getDeclaredMethods();
            methodHashMap.put(arg,methods);
        }
        //取出实体
        Object entity = args[0];

        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        AutoFill autoFill = method.getAnnotation(AutoFill.class);
        OperationType value = autoFill.value();

        //准备公共数据
        LocalDateTime localDateTime = LocalDateTime.now();
        Long Id = BaseContext.getCurrentId();

        //逻辑实现插入值
        if(value == OperationType.INSERT){
            Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
            Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class);
            Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);

            setCreateTime.invoke(entity,localDateTime);
            setCreateUser.invoke(entity,Id);
            setUpdateTime.invoke(entity,localDateTime);
            setUpdateUser.invoke(entity,Id);

        }else if(value == OperationType.UPDATE){

            Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);

            setUpdateTime.invoke(entity,localDateTime);
            setUpdateUser.invoke(entity,Id);
        }

        //获取这个枚举类型的值

//        //填充这个hashMap
//        if (value.equals(OperationType.INSERT)) {
//            for (Map.Entry<Object, Method[]> entry : methodHashMap.entrySet()) {
//
//                Object key = entry.getKey();
//                for (Method method1 : entry.getValue()) {
//                    if (method1.getName().equals(AutoFillConstant.SET_UPDATE_TIME) || method.getName().equals(AutoFillConstant.SET_CREATE_TIME)) {
//                        method1.invoke(key, LocalDateTime.now());
//                    }
//                    if (method1.getName().equals(AutoFillConstant.SET_CREATE_USER) || method.getName().equals(AutoFillConstant.SET_UPDATE_USER)) {
//                        method1.invoke(key, BaseContext.getCurrentId());
//                    }
//                }
//
//            }
//        } else if (value.equals(OperationType.UPDATE)) {
//            for (Map.Entry<Object, Method[]> entry : methodHashMap.entrySet()) {
//                Object key = entry.getKey();
//                for (Method method1 : entry.getValue()) {
//                    if (method1.getName().equals(AutoFillConstant.SET_UPDATE_TIME)) {
//                        method1.invoke(key, LocalDateTime.now());
//                    }
//                    if (method1.getName().equals(AutoFillConstant.SET_UPDATE_USER)) {
//                        method1.invoke(key, BaseContext.getCurrentId());
//                    }
//                }
//
//            }
//        }
    }
}
